from django.db import models


class Card(models.Model):
    """卡牌基础信息"""
    RARITY_CHOICES = [
        ('common', '普通'),
        ('rare', '稀有'),
        ('epic', '史诗'),
        ('legendary', '传说')
    ]
    
    ELEMENT_CHOICES = [
        ('metal', '金'),
        ('wood', '木'),
        ('water', '水'),
        ('fire', '火'),
        ('earth', '土')
    ]
    
    STAR_CHOICES = [
        (1, '1星'),
        (2, '2星'),
        (3, '3星'),
        (4, '4星'),
        (5, '5星')
    ]
    
    name = models.CharField(max_length=100, verbose_name="卡牌名称")
    description = models.TextField(verbose_name="卡牌描述")
    attack_power = models.IntegerField(verbose_name="攻击力")
    cost = models.IntegerField(default=0, verbose_name="购买价格")
    rarity = models.CharField(max_length=20, choices=RARITY_CHOICES, default='common', verbose_name="稀有度")
    star_level = models.IntegerField(choices=STAR_CHOICES, default=1, verbose_name="星级")
    element = models.CharField(max_length=10, choices=ELEMENT_CHOICES, default='fire', verbose_name="属性")
    image = models.ImageField(upload_to='card_images/', verbose_name="卡牌图片")
    is_active = models.BooleanField(default=True, verbose_name="是否可用")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    
    class Meta:
        verbose_name = "卡牌"
        verbose_name_plural = "卡牌"
        ordering = ['-created_at']
    
    def __str__(self):
        return self.name


class UserCard(models.Model):
    """用户拥有的卡牌"""
    user = models.ForeignKey('accounts.User', on_delete=models.CASCADE, verbose_name="用户")
    card = models.ForeignKey(Card, on_delete=models.CASCADE, verbose_name="卡牌")
    quantity = models.IntegerField(default=1, verbose_name="数量")
    obtained_at = models.DateTimeField(auto_now_add=True, verbose_name="获得时间")
    
    class Meta:
        verbose_name = "用户卡牌"
        verbose_name_plural = "用户卡牌"
        unique_together = ['user', 'card']
    
    def __str__(self):
        return f"{self.user.nickname} - {self.card.name} x{self.quantity}"
